//
//  ViewController.m
//  谓词
//
//  Created by apple on 15/8/26.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    Student *student = [[Student alloc] init];
    student.age = 23;
    student.name = @"Zhangsan";
    student.books = @[@"红楼梦",@"水浒传",@"西游记"];
    
    
    Student *stu = [[Student alloc] init];
    stu.age = 20;
    stu.name = @"Lisi";
#if 0
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age >= 20"];
    if ([predicate evaluateWithObject:stu])
    {
        NSLog(@"哥已经有20了");
    }
    else
    {
        NSLog(@"还没有20，不过我明年就20岁了");
    }
#endif
    
#if 0
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN {'Zhangsan','Lisi','Wangwu'}"];
    if ([predicate evaluateWithObject:student])
    {
        NSLog(@"Zhangsan在这呢！");
    }
    else
    {
        NSLog(@"Zhangsan不在这！");
    }
#endif
    
#if 0
    
//    1、BEGINSWITH －－》表示以一个字母或者以一个字符串开头；
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'Z'"];
    if ([predicate evaluateWithObject:student])
    {
        NSLog(@"张三是姓张的");
    }
    else
    {
        NSLog(@"张三居然不姓张？");
    }
    
#endif
    
#if 0

//    CONTAINS-->表示包含一个或多个多字符－－》区分大小写；
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'san'"];
    if ([predicate evaluateWithObject:student])
    {
        NSLog(@"yes");
    }
    else
    {
        NSLog(@"no");
    }
#endif
    
#if 0
    
//    ENDSWITH-->表示字符串是以一个或者多个字符结尾；
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'san'"];
    if ([predicate evaluateWithObject:student])
    {
        NSLog(@"yes");
    }
    else
    {
        NSLog(@"no");
    }
#endif
    
#if 0

    //1、？表示任意的一个字符，
    //2、＊表示一个或任意多个字符；
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE '?hangsan'"];
    if ([predicate evaluateWithObject:student])
    {
        NSLog(@"yes!");
    }
    else
    {
        NSLog(@"no!");
    }
#endif
    
#if 0   //-----------------------------------------------
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY books LINK[c] '水*'"];
    if ([predicate evaluateWithObject:student])
    {
        NSLog(@"yes");
    }
    else
    {
        NSLog(@"no");
    }
#endif
    
#if 0
    
//    SELF[大小写不敏感]代表对象本身；
//    like［大小写不敏感］like指定搜索模式，［与通配符一起用于模糊查询／过滤］；
    
    NSArray *array = @[@"hello",@"world",@"hi",@"welcome",@"her"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE 'h*'"];
    NSArray *result = [array filteredArrayUsingPredicate:predicate];
    NSLog(@"%@",result);

    
#endif
    
#if 0
    
//    与对象比较，用bool来判断；
    NSString *str = @"hello world ! welcome in  BeiJing";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE 'h*'"];
    BOOL isBeginWith_h = [predicate evaluateWithObject:str];
    if (isBeginWith_h)
    {
        NSLog(@"YES");
    }
    
#endif
    
#if 1
    Person *person1 = [[Person alloc] initWithName:@"张三" age:11];
    Person *person2 = [[Person alloc] initWithName:@"李四" age:21];
    Person *person3 = [[Person alloc] initWithName:@"王五" age:31];
    Person *person4 = [[Person alloc] initWithName:@"马六" age:41];
    
    NSArray *array = @[person1,person2,person3,person4];

    //寻找name为张三的对象
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS '三'"];
    NSArray *result =[array filteredArrayUsingPredicate:predicate];
   
//    对象打印出来只是个地址；
    NSLog(@"%@",result);
    
    for (Person *p in result)
    {
        NSLog(@"p.name: %@",p.name);
    }
    
#endif
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
